﻿using Steamworks;
using System;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Networking;

public class SteamNetworkConnection : NetworkConnection
{
    public CSteamID remoteId;
    public CSteamID selfId;

    public override string ToString()
    {
        return string.Format("{0}, selfId: {1} remoteId: {2}", base.ToString(), this.selfId, this.remoteId);
    }

    public override void TransportRecieve(byte[] bytes, int numBytes, int channelId)
    {
        if (this.isDebugging)
        {
            NetworkReader reader = new NetworkReader(bytes);
            reader.ReadUInt16();
            short num = reader.ReadInt16();
            object[] args = new object[] { bytes.Length, numBytes, channelId, num, this.selfId, this.remoteId };
            Debug.Log(string.Format("{4} TransportRecieve from {5}, length:{0} num:{1} channel:{2} msgtype:{3}", args));
        }
        base.TransportRecieve(bytes, numBytes, channelId);
    }

    public override bool TransportSend(byte[] bytes, int numBytes, int channelId, out byte error)
    {
        if (this.isDebugging)
        {
            NetworkReader reader = new NetworkReader(bytes);
            reader.ReadUInt16();
            short num = reader.ReadInt16();
            object[] args = new object[] { bytes.Length, numBytes, channelId, num, this.selfId, this.remoteId };
            Debug.Log(string.Format("{4} TransportSend to {5}, length:{0} num:{1} channel:{2} msgtype:{3}", args));
        }
        error = 0;
        EP2PSend send = EP2PSend.k_EP2PSendReliable;
        if (channelId >= 1)
        {
            send = EP2PSend.k_EP2PSendUnreliableNoDelay;
        }
        return SteamNetworking.SendP2PPacket(this.remoteId, bytes, (uint) numBytes, send, channelId);
    }

    private bool isDebugging
    {
        get
        {
            return SteamNetworkManager.isDebugging;
        }
    }
}

